define(["require", 'config/appregister',
	'business/postbar/service/postbarhttp',
	"jquery",
	"business/services/filter",
	"business/home/directives/editorDirective",
	"business/home/services/filterService"], function(require, app, httpServices) {
	app.controller('wikilistCtrl', function($scope,$rootScope, $http, $state, $stateParams, DetailFilter) {
		$scope.$parent.isDetailPage = false;
		$scope.$parent.isMainpage = true;
		$scope.listParams = {
			currentTcId: "",
			download: false,
			time: false,
			pageNo: 1,
			pageSize: 10,
			all: false,
			typeId: "",
			scId: "",
			dcId: "",
			keyWord: "",
			//dcName scName 用于加入缓存 在详情中显示面包树导航
			dcName: "",
			scName: ""
		};

		function getListModel() {
			$scope.paginationConf = {
				currentPage: $scope.listParams.pageNo,
				totalItems: $scope.totalItem,
				//每页预览条数
				itemsPerPage: $scope.listParams.pageSize,
				//分页总数
				numberOfPages: Math.ceil($scope.totalItem / $scope.listParams.pageSize),
				// 实际分页长度
				pagesLength: 5,
				perPageOptions: [10, 20, 30, 40, 50],
				onChange: function(currentPage) {
					if(currentPage) {
						$scope.paginationConf.currentPage = currentPage;
					}
					$scope.list = [];
					$scope.listParams.pageNo = $scope.paginationConf.currentPage;
					getList();
				}
			};
		};
		//未登录动作
		$scope.gotoiask = function() {
			if($rootScope.userLoginInfo.logged) {
				$state.go("eng.iask");
			}else {
				$(".tip-box").show();
			}
		};
		$scope.tologin = function() {
			$state.go("login");
		};
		$scope.toreg = function() {
			$state.go("login",{type:"register"});
		};

		$scope.change = function($index) {
			var thenum = $index;
			if(thenum % 2 == 0) {
				return {
					"background-color": "#fff"
				}
			} else {
				return {
					"background-color": "#f5f5f5"
				};
			};
		};
		//
		$scope.pdd = true;
		$scope.showli1 = function() {
			if($scope.pdd) {
				$(".mutate ol").hide();
				$('.pa1').css("display", "block");
				$(".mutate h3").css("color","#666");
				var theh3 = $(".mutate h3")[0];
				$(theh3).css("color","#30a3ff");
				$scope.pdd = false;
			} else {
				$scope.pdd = true;
				$('.pa1').css("display", "none");
			};
		};
		
		$scope.showli2 = function() {
			if($scope.pdd) {
				$(".mutate ol").hide();
				$('.pa2').css("display", "block");
				$(".mutate h3").css("color","#666");
				var theh3 = $(".mutate h3")[1];
				$(theh3).css("color","#30a3ff");
				$scope.pdd = false;
			} else {
				$scope.pdd = true;
				$('.pa2').css("display", "none");
			};
		};
		$scope.showli3 = function() {
			if($scope.pdd) {
				$(".mutate ol").hide();
				$('.pa3').css("display", "block");
				$(".mutate h3").css("color","#666");
				var theh3 = $(".mutate h3")[2];
				$(theh3).css("color","#30a3ff");
				$scope.pdd = false;
			} else {
				$scope.pdd = true;
				$('.pa3').css("display", "none");
			};
		};
		$scope.showli4 = function() {
			if($scope.pdd) {
				$(".mutate ol").hide();
				$('.pa4').css("display", "block");
				$(".mutate h3").css("color","#666");
				var theh3 = $(".mutate h3")[3];
				$(theh3).css("color","#30a3ff");
				$scope.pdd = false;
			} else {
				$scope.pdd = true;
				$('.pa4').css("display", "none");
			};
		};
		
		
		pdd = 0;
		//
		$scope.jump = function(a) {
			$state.go("eng.postbardeta",{postbarId:a});		
		};
		//

		//右边list
		var tcid = 0,
			scid = 0,
			dcid = 0,
			solve = 0,
			pages = 1,
			pagesize = 10,
			all = false,
			boon = false;

		function getList() {
			momo = 0;
			var req = {
				type:'GET',
				uri:'getList',
				data:{
					tcId:tcid,
					scId:scid,
					dcId:dcid,
					solve:solve,
					reward:boon,
					pageNo:$scope.listParams.pageNo,
					pageSize:$scope.listParams.pageSize,
					all:all
				}
			};
			httpServices.postbarlistHttpService(req).success(function(data){
					if(!data)return;
					if(data.code==0) {	
						$scope.$apply(function() {
							if(data.data.list.length==0) {
								$(".nodata").show();
								$(".wiki-list").hide();
								$(".fenyebox").hide();
							}else {
								$(".wiki-list").show();
								$(".fenyebox").show();
								$(".nodata").hide();
								$scope.qqq = data.data.list;
								for(var item in $scope.qqq){
									if($scope.qqq[item].solve==1){
										$scope.qqq[item].solveHtml='待解决'
									}else{
										$scope.qqq[item].solveHtml='已解决'
									}
								}
								$scope.totalItem = data.data.Pagination.totalCount;
								$scope.listParams.pageNo = data.data.Pagination.pageNo;
								$scope.listParams.pageSize = data.data.Pagination.pageSize;
								getListModel();								
							}
						});						
					}else {
						alert(data.code);
					};
			}).error(function(){
				alert("百问百达列表请求失败！")
			});
		};
		getList();
		//右边盒子头部选项卡

		//1级分类
		var req = {
			type:'GET',
			uri:'getTopCategory'
		};
		httpServices.oneclassHttpService(req).success(function(data){
			if(data.code==0){
				$scope.$apply(function() {
					$scope.aaa = data.data;
				})				
			}else{
				alert(data.msg);
			}
		}).error(function(){
			alert("一级分类请求失败");
		});

		$scope.isShow = [];
		$scope.gay = 0;
		$scope.getone = function(index, tc) {
			var theh1 = $(".oneclick")[index];
			$(".oneclick").css("color","#666");
			$(theh1).css("color","#30a3ff");
			$scope.isShowInter = false;
			$scope.isShowOther = false;
			$scope.gay++;
			$scope.isShow = [];
			tcid = tc;
			scid = 0;
			dcid = 0;
			$scope.listParams.pageNo=1;
			getList();		
			var req = {
				type:'GET',
				uri:'getSecondaryCategory',
				data:{
					tcId:tcid
				}
			};
			httpServices.getscidHttpService(req).success(function(data){
				if(!data)return;
				$scope.$apply(function() {
					$scope.bbb = data.data;
					if($scope.gay == 1) {
						var thedom = sessionStorage.getItem("dom");
						if(thedom && thedom == index) {
							$scope.gay = 0;
							$scope.isShow[index] = false;
							sessionStorage.removeItem("dom");
						}else {
							sessionStorage.setItem('dom',index);
							$scope.isShow[index] = true;
						}
					}else {
						var thedom2 = sessionStorage.getItem("dom");
						if(thedom2 && thedom2 == index) {
							$scope.gay = 0;
							$scope.isShow[index] = false;
							sessionStorage.removeItem("dom");
						}else {
							sessionStorage.setItem('dom',index);
							$scope.isShow[index] = true;
						}
					}
				})				
			}).error(function(data){
				alert("查2级分类请求失败")
			});
		};
		$scope.isShows = [];
		var lala = 0;
		$scope.isShowInter = false;
		$scope.isShowOther = !$scope.isShowInter;

		//点击二级分类请求三级分类
		$scope.gettwo = function(index, sc, tcId) {
			var theh1 = $(".twoclick")[index];
			$(".twoclick").css("color","#666");
			$(theh1).css("color","#30a3ff");
			if(index == 0) {
				$scope.if4 = false;
				$scope.if42 = false;
				$scope.h31 = "国家";
				$scope.h32 = "专业";
				$scope.h33 = "种类";				
			}else if(index == 1){
				$scope.if4 = false;
				$scope.if42 = false;
				$scope.h31 = "专业";
				$scope.h32 = "内容";
				$scope.h33 = "项目模式";				
			}else {
				$scope.if4 = true;
				$scope.if42 = false;
				$scope.h31 = "经济环境";
				$scope.h32 = "法律法规";
				$scope.h33 = "合同管理";
				$scope.h34 = "宗教文化";
			}			
			if(tcId==null||tcId == '')return;
			pdd = 0;
			$('.pa1').css("display", "none");
			$('.pa2').css("display", "none");
			$('.pa3').css("display", "none");
			$('.pa4').css("display", "none");
			
			$scope.isShows[index] = true;
			lala++;
			$scope.isShows = [];
			scid = sc;
			tcid = tcId;
			dcid = 0;
			$scope.listParams.pageNo=1;
			getList();
			
			var zhuanye = [];
			var leirong = [];
			var moshi = [];
			var zongj = [];
			
			var req = {
				type:'GET',
				uri:'getDetailCategory',
				data:{
					scId:scid,
					tcId:tcid
				}
			};
			httpServices.getdcidHttpService(req).success(function(data){
				if(!data)return;
				if(data.data==null)return;
				$scope.$apply(function(){
					var thedata = data.data;
					if(tcId == 2) {
							//$scope.isShowInter = true;
							$scope.ccc = null;
							//$scope.isShowInter = true;
							//$scope.isShowOther = !$scope.isShowInter;
//							$scope.getCurrenttData = DetailFilter.getDetail(data.data);
						var four = thedata;
						for(i in four) {
							if(four[i].dcType == 1 || four[i].dcType == 4 || four[i].dcType == 7) {
								zhuanye.push(four[i]);
							} else if(four[i].dcType == 2 || four[i].dcType == 5 || four[i].dcType == 8) {				
								leirong.push(four[i]);
							} else if(four[i].dcType == 3 || four[i].dcType == 6 || four[i].dcType == 9) {
								moshi.push(four[i]);
							} else if(four[i].dcType == 10) {
								zongj.push(four[i]);
							}
						}
						$scope.zy = zhuanye;
						$scope.lr = leirong;
						$scope.ms = moshi;
						$scope.duo = zongj;
					} else {
						var u1 = [];
						var u2 = [];
						var u3 = [];
						var u4 = [];
						var u5 = [];
						var u6 = [];
						var u7 = [];
						var u8 = [];
						var u9 = [];
						var u10 = [];
						var uu = [];

						for(i in thedata) {
							if(thedata[i].dcType == 1) {
								u1.push(thedata[i]);
							} else if(thedata[i].dcType == 2) {
								u2.push(thedata[i]);
							} else if(thedata[i].dcType == 3) {
								u3.push(thedata[i]);
							} else if(thedata[i].dcType == 4) {
								u4.push(thedata[i]);
							} else if(thedata[i].dcType == 5) {
								u5.push(thedata[i]);
							} else if(thedata[i].dcType == 6) {
								u6.push(thedata[i]);
							} else if(thedata[i].dcType == 7) {
								u7.push(thedata[i]);
							} else if(thedata[i].dcType == 8) {
								u8.push(thedata[i]);
							} else if(thedata[i].dcType == 9) {
								u9.push(thedata[i]);
							} else if(thedata[i].dcType == 10) {
								u10.push(thedata[i]);
							} else if(thedata[i].dcType == 0) {
								uu.push(thedata[i]);
							}
						}
							$scope.isShowInter = false;
							$scope.isShowOther = !$scope.isShowInter;
							$scope.getCurrenttData = null;
							$scope.ccc = uu;
					}
					/*if(lala == 1) {
						$scope.isShows[index] = true;
					} else {
						lala = 0;
						$scope.isShows[index] = false;
					}*/
				});			
			}).error(function(data){
				console.log("三级分类请求失败");
			});
		};

		$scope.getthree = function(dc, sc, tc, index) {
			var theh1s = $(".threeclick")[index];
			$(".threeclick").css("color","#999");
			$(theh1s).css("color","#30a3ff");
			pdd = 0;
			$('.pa1').css("display", "none");
			$('.pa2').css("display", "none");
			$('.pa3').css("display", "none");
			$('.pa4').css("display", "none");
			scid = sc;
			dcid = dc;
			$scope.listParams.pageNo=1;
			getList();
		}

		$scope.getalls = function() {
			solve = 0;
			boon = false;
			all = false;
			$scope.listParams.pageNo = 1;
			getList();
			//css
			$(".over").css("border-top-color", "#fff");
			$(".over").css("color", "#000");
			$(".all").css("border-top-color", "#30a3ff");
			$(".all").css("color", "#30a3ff");
			$(".price").css("border-top-color", "#fff");
			$(".price").css("color", "#000");
			$(".none").css("border-top-color", "#fff");
			$(".none").css("color", "#000");

		}

		$scope.getover = function() {
			solve = 2;
			$scope.listParams.pageSize = 10;
			$scope.listParams.pageNo = 1;
			getList();
			//css
			$(".over").css("border-top-color", "#30a3ff");
			$(".over").css("color", "#30a3ff");
			$(".all").css("border-top-color", "#fff");
			$(".all").css("color", "#000");
			$(".price").css("border-top-color", "#fff");
			$(".price").css("color", "#000");
			$(".none").css("border-top-color", "#fff");
			$(".none").css("color", "#000");
		};

		$scope.getnone = function() {
			solve = 1;
			boon = false;
			$scope.listParams.pageNo = 1;
			getList();
			//css
			$(".over").css("border-top-color", "#fff");
			$(".over").css("color", "#000");

			$(".all").css("border-top-color", "#fff");
			$(".all").css("color", "#000");
			$(".price").css("border-top-color", "#fff");
			$(".price").css("color", "#000");
			$(".none").css("border-top-color", "#30a3ff");
			$(".none").css("color", "#30a3ff");
		};

		$scope.getprice = function() {
			boon = true;
			solve = 0;
			$scope.listParams.pageNo = 1;
			getList();
			//css
			$(".over").css("border-top-color", "#fff");
			$(".over").css("color", "#000");
			$(".all").css("border-top-color", "#fff");
			$(".all").css("color", "#000");
			$(".price").css("border-top-color", "#30a3ff");
			$(".price").css("color", "#30a3ff");
			$(".none").css("border-top-color", "#fff");
			$(".none").css("color", "#000");
		};
		$scope.curAnswer = "" ;
		$scope.gaibian = function(index, item) {
			var thelispan = $(".wiki-list li");
			var thiss = thelispan[index];
			var the = $(thiss).find("span")[1]
			var itm = item;
			var zhege = $(thiss).find("div")[0];
			$(".item-hover").css("opacity", "0");
			if(item.closeStatus == 1) {
				$(".item-hover").css("opacity", "0");
			} else {
				$(the).css("opacity", "1");
				var momo = true;
				$scope.clickthe = function(num,item,index){
					if($rootScope.userLoginInfo.logged) {
						if(num==1)return;
						if(momo) {
							$scope.curAnswer  = item.postbarId ;
							$(".div-kuai").hide();
							$(".item-hover").html("快速回答");
							$(thelispan).css("height","44px");
							var thedom = $(".item-hover")[index];
							$(thedom).html("取消回答");
							$(zhege).show();
							$(thiss).css("height", "342px");
							momo = false;
						} else {
							var thedoms = $(".item-hover")[index];
							$(thedoms).html("快速回答");							
							$(zhege).hide();
							$(thiss).css("height", "44px");
							$(".div-kuai").hide();
							$(thelispan).css("height","44px");
							momo = true;
						}						
					}else {
						$(".tip-box").show();
					}

				};
			}
		};
		$scope.editorContent = '' ;
		
		$scope.subAnswer = function(a,b) {
			if($scope.editorContent.length > 4000){
				alert("最多输入2000个字符，您超过了限制，请做删减");
				return;
			}			
			if($scope.editorContent.length < 9){
				alert("回答不能为空")
				return;
			}else {
				if($rootScope.userLoginInfo.data.userId == b) {
					alert("自己不能回答自己的问题");
					getList();
				}else {
					var req = {
						type:'POST',
						uri:'addReply',
						data:{
							postbarId: a,
							content: $scope.editorContent,
							seed:localStorage.seed,
							authToken: localStorage.authToken							
						}
					};
					$.ajax({
						type:'post',
						dataType:'json',
						data:req.data,
						url:"http://"+allUrl+":8086/elink_postbar/api/reply/addReply",
						success:function(data) {
							if(!data)return;
							$scope.$apply(function(){
								if(data.code==0) {
									alert("快速回复成功");
									getList();					
								}else {
									alert(data.msg);
								}
							});
						},	
						error:function() {
							alert("快速回复请求失败");
						}
					});									
				}
					
			}
		
		};
		
		$scope.getsan = function(a, b, event) {
			$(".mutate li").css("color","#666");
			var  keyValue = event.srcElement ? 'srcElement' : 'target';
			$(event[keyValue]).css("color","#30a3ff");
			scid = b;
			dcid = a;
			getList();
		};
		
		
		
		
		
		$scope.noxbtn = function() {
			$(".tip-box").hide();
		}
		
		
		
		
		
		
		
		
		
	})
})